// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.Lua;

import se.krka.kahlua.converter.JavaToLuaConverter;
import se.krka.kahlua.converter.KahluaConverterManager;
import se.krka.kahlua.converter.LuaToJavaConverter;

public final class KahluaNumberConverter {
    private KahluaNumberConverter() {
    }

    public static void install(KahluaConverterManager kahluaConverterManager) {
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Long>() {
            public Long fromLuaToJava(Double _double, Class<Long> var2) {
                return _double.longValue();
            }

            public Class<Long> getJavaType() {
                return Long.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Integer>() {
            public Integer fromLuaToJava(Double _double, Class<Integer> var2) {
                return _double.intValue();
            }

            public Class<Integer> getJavaType() {
                return Integer.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Float>() {
            public Float fromLuaToJava(Double _double, Class<Float> var2) {
                return new Float(_double.floatValue());
            }

            public Class<Float> getJavaType() {
                return Float.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Byte>() {
            public Byte fromLuaToJava(Double _double, Class<Byte> var2) {
                return _double.byteValue();
            }

            public Class<Byte> getJavaType() {
                return Byte.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Character>() {
            public Character fromLuaToJava(Double _double, Class<Character> var2) {
                return (char)_double.intValue();
            }

            public Class<Character> getJavaType() {
                return Character.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addLuaConverter(new LuaToJavaConverter<Double, Short>() {
            public Short fromLuaToJava(Double _double, Class<Short> var2) {
                return _double.shortValue();
            }

            public Class<Short> getJavaType() {
                return Short.class;
            }

            public Class<Double> getLuaType() {
                return Double.class;
            }
        });
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Double.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Float.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Integer.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Long.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Short.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Byte.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(Character.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(double.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(float.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(int.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(long.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(short.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(byte.class));
        kahluaConverterManager.addJavaConverter(new KahluaNumberConverter.NumberToLuaConverter(char.class));
        kahluaConverterManager.addJavaConverter(new JavaToLuaConverter<Boolean>() {
            public Object fromJavaToLua(Boolean _boolean) {
                return _boolean;
            }

            public Class<Boolean> getJavaType() {
                return Boolean.class;
            }
        });
    }

    private static final class DoubleCache {
        static final int low = -128;
        static final int high = 10000;
        static final Double[] cache = new Double[10129];

        public static Double valueOf(double _double) {
            return _double == (double)((int)_double) && _double >= -128.0 && _double <= 10000.0 ? cache[(int)(_double + 128.0)] : new Double(_double);
        }

        static {
            int int0 = -128;

            for (int int1 = 0; int1 < cache.length; int1++) {
                cache[int1] = new Double((double)(int0++));
            }
        }
    }

    private static final class NumberToLuaConverter<T extends Number> implements JavaToLuaConverter<T> {
        private final Class<T> clazz;

        public NumberToLuaConverter(Class<T> _class) {
            this.clazz = _class;
        }

        public Object fromJavaToLua(T number) {
            return number instanceof Double ? number : KahluaNumberConverter.DoubleCache.valueOf(number.doubleValue());
        }

        public Class<T> getJavaType() {
            return this.clazz;
        }
    }
}
